/**
 * Please modify this class to meet your needs
 * This class is not complete
 */
package at.ac.tuwien.infosys.ait09.registry;

import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.namespace.QName;
import org.xmlsoap.schemas.ws._2004._08.addressing.AttributedQName;
import org.xmlsoap.schemas.ws._2004._08.addressing.AttributedURI;
import org.xmlsoap.schemas.ws._2004._08.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws._2004._08.addressing.ServiceNameType;

/**
 * This class was generated by Apache CXF 2.2.3
 * Tue Jan 12 00:45:04 CET 2010
 * Generated source version: 2.2.3
 * 
 */
/*-------------------------------------------------------------------------------------------------
 * <pre>
 * &lt;complexType name="EndpointReferenceType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Address" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}AttributedURI"/>
 *         &lt;element name="ReferenceProperties" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}ReferencePropertiesType" minOccurs="0"/>
 *         &lt;element name="ReferenceParameters" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}ReferenceParametersType" minOccurs="0"/>
 *         &lt;element name="PortType" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}AttributedQName" minOccurs="0"/>
 *         &lt;element name="ServiceName" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}ServiceNameType" minOccurs="0"/>
 *         &lt;any processContents='lax' namespace='##other' maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *       &lt;anyAttribute processContents='lax' namespace='##other'/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
----------------------------------------------------------------------------------------------------*/
@javax.jws.WebService(serviceName = "RegistryService",
portName = "RegistryServicePort",
targetNamespace = "http://www.infosys.tuwien.ac.at/ait09/registry",
endpointInterface = "at.ac.tuwien.infosys.ait09.registry.RegistryService")
public class RegistryServiceImpl implements RegistryService {

    private static final Logger LOG = Logger.getLogger(RegistryServiceImpl.class.getName());

    /* (non-Javadoc)
     * @see at.ac.tuwien.infosys.ait09.registry.RegistryService#queryNotificationService(at.ac.tuwien.infosys.ait09.registry.CustomerType  parameters )*
     */
    public at.ac.tuwien.infosys.ait09.registry.RefType queryNotificationService(CustomerType customer) { // (CustomerType parameters)

        LOG.info("Executing operation queryNotificationService");
        System.out.println(customer.getId() + " " + customer.getName());
        
        // here set the Endpoint properties that are same for both servicies
        EndpointReferenceType endpointServiceType = new EndpointReferenceType();

        //ReferenceType to ruturn. It may be either sms or mail service RefType
        RefType smsMailServiceRefType = new RefType();

        ServiceNameType serviceName = new ServiceNameType();
        serviceName.setPortName("NotificationPT");
        serviceName.setValue(new QName("http://infosys.tuwien.ac.at/ait09/ass1/dto/notification", "NotificationService"));
        endpointServiceType.setServiceName(serviceName);

        AttributedQName portType = new AttributedQName();
        //QName(String namespaceURI, String localPart)
        QName ptQName = new QName("http://infosys.tuwien.ac.at/ait09/ass1/dto/notification", "NotificationService");
        portType.setValue(ptQName);
        endpointServiceType.setPortType(portType);

        AttributedURI addressURI = new AttributedURI();

        // here comes the implementation
        int hc = customer.getName().hashCode() + customer.getId().hashCode();
        if ((hc % 2) == 0) {
            // return SMS service

            System.out.println("[ServiceRegistry]:: return SMS service");

            // setAddress to smsservice
            addressURI.setValue("http://localhost:8088/smsservice");
            endpointServiceType.setAddress(addressURI);

            // Here I don't know what to do. I'll leave it like that for the moment
            /*
            endpointServiceType.setReferenceParameters(null);
            endpointServiceType.setReferenceProperties(null);
             */

            //RefType smsServiceRefType = new RefType();
            smsMailServiceRefType.setEndpointReference(endpointServiceType);

            //Return smsServiceRefType with endpointReference at smsservice
            return smsMailServiceRefType;
        } else {
            //return mail service
            System.out.println("[ServiceRegistry]:: return mail service");

            //AttributedURI addressURI = new AttributedURI();
            // setAddress to smsservice
            addressURI.setValue("http://localhost:8089/mailservice");
            endpointServiceType.setAddress(addressURI);

            //RefType mailServiceRefType = new RefType();
            smsMailServiceRefType.setEndpointReference(endpointServiceType);

            //Return smsServiceRefType with endpointReference at smsservice
            return smsMailServiceRefType;
        }
    }
}
